package com.yjhb.android.sbz.tool;

import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.yjhb.android.sbz.Conf;
import com.yjhb.android.sbz.App;
import com.yjhb.android.sbz.entity.UserInfo;
import com.jojo.android.zxlib.tool.ZxSPTool;

/**
 * 创建者 zx
 * 创建时间 2020/11/24 9:17
 */
public class UserTool {
    private final String TAG = "UserTool";

    /**
     * 从本地取用户信息
     *
     * @return
     */
    public static UserInfo fetchUserInfo() {
        try {
            UserInfo userInfo = null;
            //取用户信息
            String userBean = (String) ZxSPTool.getParam(App.getInstance(), Conf.SP_USER_INFO, "");
            if (userBean != null) {
                userInfo = new Gson().fromJson(userBean, UserInfo.class);
            }
            return userInfo;
        }catch (Exception e){

        }
        return null;

    }


    /**
     * 保存用户信息到本地
     *
     * @param userInfo
     */
    public static void saveUserInfo(UserInfo userInfo) {
        if (userInfo == null) return;
        String userJson = new Gson().toJson(userInfo);
        ZxSPTool.setParam(App.getInstance(), Conf.SP_USER_INFO, userJson);
    }

    public static void saveConfig(JSONObject config) {
        if (config == null) return;
        String configJson = new Gson().toJson(config);
        ZxSPTool.setParam(App.getInstance(), Conf.SP_USER_INFO, configJson);
    }

    /**
     * 从本地取配置信息
     *
     * @return
     */
    public static JSONObject fetchConfig() {
        try {
            JSONObject config = null;
            String json = (String) ZxSPTool.getParam(App.getInstance(), Conf.SP_USER_INFO, "");
            if (json != null) {
                config = JSON.parseObject(json);
            }
            return config;
        }catch (Exception e){
        }
        return null;

    }


    public static String UUID() {
        String uuid = (String) ZxSPTool.getParam(App.getInstance(), Conf.SP_USER_UUID, "");
        return uuid;
    }

    public static void saveUUID(String UUID){
        if (TextUtils.isEmpty(UUID))
            return;
        ZxSPTool.setParam(App.getInstance(), Conf.SP_USER_UUID, UUID);
    }

    public static void clearUserInfo() {
        ZxSPTool.clearByKey(App.getInstance(), Conf.SP_USER_INFO);
    }

}
